package cn.gcxy.handler;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gcxy.entity.User;
import cn.gcxy.mapper.UserMapper;
import cn.gcxy.service.UserService;

@Controller
@RequestMapping("user")
public class UserHandler {
	
	@Autowired
	@Qualifier("userService")
	UserService userService;
	
	@ResponseBody
	@RequestMapping("editUser")
	public boolean editUser(@RequestParam("id") String id,@RequestParam("sex") String sex, @RequestParam("name") String name,
			@RequestParam("birthday") String birthday,@RequestParam("email") String email,@RequestParam("phone") String phone,
			@RequestParam("address") String address,@RequestParam("emailnum") int emailnum, HttpSession session) {
		
		User user = new User(id,name,sex,address, phone, email, emailnum, birthday);
		//if()
		if(userService.editUser(user)) {
			session.setAttribute("user", userService.queryUserById(id));
			return true;
		}else
			return false;
	}
	
	@ResponseBody
	@RequestMapping("editPwd")
	public boolean editPwd(@RequestParam("id") String id,@RequestParam("oldPwd") String oldPwd, @RequestParam("newPwd") String newPwd) {
		return userService.editPwd(id,oldPwd,newPwd);	
	}	
	
	@ResponseBody
	@RequestMapping("addUser")
	public boolean addUser(@RequestParam("id") String id,@RequestParam("name") String name,@RequestParam("password") String password) {
		return userService.addUser(id,name,password);	
	}

	@ResponseBody
	@RequestMapping("editState")
	public boolean editState(@RequestParam("id") String id,@RequestParam("state") int state) {
		return userService.editState(id,state);	
	}
	
}
